package com.terry.proxy.util;

import cn.hutool.core.thread.ThreadFactoryBuilder;

import java.util.concurrent.*;

/**
 * 线程池执行
 *
 * @author allen
 * @date 2022/09/28
 */
public class ThreadPoolExecutorUtil {
    private static final int CORE_POOL_SIZE = 5;
    private static final int MAX_POOL_SIZE = 400;
    private static final int KEEP_ALIVE_SECONDS = 60;
    private static final BlockingQueue<Runnable> RUNNABLE_ARRAY_BLOCKING_QUEUE = new ArrayBlockingQueue<>(20);
    private static final int QUEUE_CAPACITY = Integer.MAX_VALUE;
    private static final ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(10);
    private static final ThreadPoolExecutor THREAD_POOL_EXECUTOR;
    static {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNamePrefix("thread-pool-%d").build();
        THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS, RUNNABLE_ARRAY_BLOCKING_QUEUE, namedThreadFactory);
    }
    public static void execute(Runnable command) {
        THREAD_POOL_EXECUTOR.execute(command);
    }
    public static void runSchedule(Runnable runnable, Integer delayTime, TimeUnit timeUnit) {
        scheduled.schedule(runnable, delayTime, timeUnit);
    }
}